<?php
App::uses('AppController', 'Controller');

class ContratosController extends AppController
{

    public $uses = array('Contrato');
    public $layout = 'epcm';

    public function index()
    {
        $contratos = $this->Contrato->find('all', array(
            'recursive' => -1,
            //'fields'=>array('')
        ));
        $this->set(compact('contratos'));
    }

    function insertar(){
        if(!empty($this->data)){
            if($this->Contrato->save($this->data)){
                $this->Session->setFlash('Se Guardo Correctamente!!!');
                $this->redirect(array('action'=>'index')); 
            }
            else{
                $this->Session->setFlash('Error al Guardar consulte con el Administrador de Sistema');
            }
        }
        
    }      

    function eliminar($id=null){
        $this->Contrato->id=$id;
        $this->data=$this->Contrato->read();
        if(!$id){
            $this->Session->setFlash('No existe el Contrato a eliminar');
            $this->redirect(array('action' =>'index'));
        }
        else{
            if($this->Contrato->delete($id)){
                $this->Session->setFlash('Se elimino el Contrato '.$this->data['COntrato']['nombre']);
                $this->redirect(array('action' =>'index'));
            }
            else{
                $this->Session->setFlash('Error al eliminar');
            }
        }
    }
	
    function editar($id = null)
    {
        $this->Contrato->id = $id;
        if (!$id) {
            $this->Session->setFlash('No Existe el tipo de Plano');
            $this->redirect(array('action' => 'index'));
        }
        if (empty($this->data)) {
            $this->data = $this->Contrato->read();
            } else {
                if (empty($this->request->data['Contrato']['IAP'])) {
                $this->request->data['Contrato']['IAP'] = 0;
                }
            if ($this->Contrato->save($this->data)) {
                //debug($this->data);exit;
                $this->Session->setFlash('Se Guardo Correctamente el Contrato');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Error al Guardar el Contrato');
            }
        }

    }

    function insertarplano()
    {
        if (!empty($this->data)) {
            if ($this->Plano->save($this->data)) {
                $this->Session->setFlash('Se Guardo Correctamente!!!');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Error al Guardar consulte con el Administrador de Sistema');
            }
        }

    }
    
    public function detalle($id = null) {
        
        $detalle = $this->Contrato->find('all', array(
            'recursive' => -1,
            'conditions' => array('Contrato.id' => $id),
            'orden' => array('numero ASC')));
        
        $this->set(compact('detalle', 'id'));
        //debug($detalle);
        
        //$detallecontrato = $this->Contrato->find('all', array('recursive' => 0));
        //$this->set(compact('detallecontrato'));
    }

}
